﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static LotusIslandOrderProcessing.model.Save;

namespace LotusIslandOrderProcessing.model
{
    public  class PrinterWebSocket
    {
        private static PrinterWebSocket instance;
        private PrinterWebSocket() { }
        public static PrinterWebSocket Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new PrinterWebSocket();
                }
                return instance;
            }
        }

        public int Init = 0;
        public void DyCX1()
        {
            try
            {
                using (var cvt = new JmPrint.Converter(JmPrint.PrinterType.Color, "revert=0:duplex=120"))
                {
                    using (var usbmgr = new USB.Manager())
                    {
                        var dd = usbmgr.Open(0);
                        try
                        {
                            var dd3 = usbmgr.GetStatus2(0xF4, 4);
                            Console.WriteLine(dd3.Count());
                            if (dd3.Count() > 3)
                            {
                                var msg = dd3[0] + " " + dd3[1] + " " + dd3[2] + " " + dd3[3];
                                Console.WriteLine(dd3[0] + " " + dd3[1] + " " + dd3[2] + " " + dd3[3]);
                                if (msg == "8 0 0 0")
                                {
                                    CoffeeMatters.Instance.IslandMatter54 = 1;
                                }
                                else
                                {
                                    CoffeeMatters.Instance.IslandMatter54 = 0;
                                }
                            }
                        }
                        catch (Exception)
                        {
                        }
                        usbmgr.Close();
                        Console.WriteLine("ending");
                    }

                }
            }
            catch (Exception)
            {

            }
        }
    
        public void DyCS1(string path)
        {
            try
            {
                using (var cvt = new JmPrint.Converter(JmPrint.PrinterType.Color, "revert=0:duplex=120"))
                {
                    var imgPath = path;
                    cvt.ConvertImage(imgPath, 600, 600);
                    var data = cvt.GetData();
                    using (var usbmgr = new USB.Manager())
                    {
                        try
                        {
                            var dd = usbmgr.Open(0);
                            var dd2 = usbmgr.Write(data);
                        }
                        catch (Exception)
                        {
                        }
                        try
                        {
                            Thread.Sleep(15000);
                            while (true)
                            {
                                var dd3 = usbmgr.GetStatus2(0xF4, 4);
                                Console.WriteLine(dd3.Count());
                                if (dd3.Count() > 3)
                                {
                                    var msg = dd3[0] + " " + dd3[1] + " " + dd3[2] + " " + dd3[3];
                                    Console.WriteLine(dd3[0] + " " + dd3[1] + " " + dd3[2] + " " + dd3[3]);
                                    if (msg == "8 0 0 0")
                                    {
                                        break;
                                    }
                                }
                                //Init++;
                                //if (Init == 60)
                                //{
                                //    break;
                                //}
                                Thread.Sleep(1000);
                            }
                        }
                        catch (Exception)
                        {
                            Thread.Sleep(15000);
                        }
                        usbmgr.Close();
                        Thread.Sleep(4000);
                        Console.WriteLine("ending");
                    }

                }
            }
            catch (Exception)
            {
            }
        }
      
    }
}
